﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;

using CCWin;
using BS_2.Class;
using System.Data.SQLite;

namespace BS_2
{
    public partial class Form_DataEdit : CCWin.Skin_DevExpress
    {
        
        public Form_DataEdit()
        {
            //skinTabPage2.Parent = null;
            //this.skinTabControl1.TabPages.Remove(skinTabPage2);
            this.ShowInTaskbar = false;
            InitializeComponent();
        }
        public void loaddata(string subname)
        {
            string sql = "select subcode from subgeo where subname='" + subname+"'";
            if (DataAccess.Run_SQL(sql)!=0)
            {
                string subcode = DataAccess.Get_Row1_Col1_Value(sql);
                sql = "select dataname,datasource,datadescribe from data where subcode=" + subcode;
                if (DataAccess.Run_SQL(sql) != 0)
                {
                    DataTable db = DataAccess.Get_DataTable(sql, "sub");
                    skinDataGridView2.DataSource = db;
                    skinDataGridView2.ClearSelection();
                }
            }
            else
            {
                MessageBoxEx.Show("子区不存在，请重新输入！");
            }
        }

        

        private void skinDataGridView2_SelectionChanged(object sender, EventArgs e)
        {
            int a = skinDataGridView2.CurrentRow.Index;
            datanamebox.Text = skinDataGridView2.Rows[a].Cells[0].Value.ToString();
            datasourcebox.Text=skinDataGridView2.Rows[a].Cells[1].Value.ToString();
            datadescribebox.Text= skinDataGridView2.Rows[a].Cells[2].Value.ToString();
            subnamebox.Text = Globale.subname;
        }

        private void subnamebox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
        }

        private void skinButton4_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBoxEx.Show("确认删除该资料吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                int a = skinDataGridView2.CurrentRow.Index;
                string dataname = skinDataGridView2.Rows[a].Cells[0].Value.ToString();
                string sql = "select datacode from data where dataname='" + dataname + "'";
                string datacode = DataAccess.Get_Row1_Col1_Value(sql);
                sql = "delete from data where datacode ='" + datacode + "'";
                DataAccess.Run_SQL(sql);
                sql = "delete from append where datacode ='" + datacode + "'";
                DataAccess.Run_SQL(sql);
                DialogResult dr1 = MessageBoxEx.Show("该资料已删除！", "提示", MessageBoxButtons.OK);
                if (dr1 == DialogResult.OK)
                {
                    if (skinComboBox1.Text != null)
                    {
                        Globale.subname = skinComboBox1.Text;
                        loaddata(Globale.subname);
                        datanamebox.Text = null;
                        datasourcebox.Text = null;
                        datadescribebox.Text = null;
                        subnamebox.Text = null;
                    }
                }
            }
        }

        private void submit_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBoxEx.Show("确认更新该资料吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                int a = skinDataGridView2.CurrentRow.Index;
                string dataname = skinDataGridView2.Rows[a].Cells[0].Value.ToString();
                string sql = "select datacode from data where dataname='" + dataname + "'";
                string datacode = DataAccess.Get_Row1_Col1_Value(sql);
                sql = "update data set dataname='"+datanamebox.Text+"',datasource='"+datasourcebox.Text+"',datadescribe='"+datadescribebox.Text+"' where datacode='" + datacode + "'";
                DataAccess.Run_SQL(sql);
                DialogResult dr1 = MessageBoxEx.Show("该资料已更新！", "提示", MessageBoxButtons.OK);
                if (dr1 == DialogResult.OK)
                {
                    if (skinComboBox1.Text != null)
                    {
                        Globale.subname = skinComboBox1.Text;
                        loaddata(Globale.subname);
                        datanamebox.Text = null;
                        datasourcebox.Text = null;
                        datadescribebox.Text = null;
                        subnamebox.Text = null;
                    }
                }
            }
        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog dialog = new OpenFileDialog())
            {
                dialog.Multiselect = false;
                if (appendtype.SelectedIndex == 0)
                {
                    dialog.Filter = "文档|*.pdf;*.pdf";
                }
                else if (appendtype.SelectedIndex == 1)
                {
                    dialog.Filter = "图片|*.jpg;*.jpg";
                }
                else if (appendtype.SelectedIndex == 2)
                {
                    dialog.Filter = "矢量压缩文件|*.zip;*.zip";
                }
                else if (appendtype.SelectedIndex == 3)
                {
                    dialog.Filter = "三维模型|*.csv;*.csv";
                }
                else if (appendtype.SelectedIndex == 4)
                {
                    dialog.Filter = "栅格数据|*.tif;*.tif";
                }
                else if (appendtype.SelectedIndex == 5)
                {
                    dialog.Filter = "剖面图压缩包|*.zip;*.zip";
                }
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        this.skinTextBox1.Text = dialog.FileName;
                        string lujing = dialog.SafeFileName;
                        /*int length = lujing.Split('\\').Length;
                        string lujing1 = lujing.Split('\\')[length - 1];*/
                        string lujing3 = lujing.Split('.')[0];
                        appendname.Text = lujing3;
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }

                }
            }
        }

        private void skinButton2_Click(object sender, EventArgs e)//进行上传
        {
            int a = skinDataGridView2.CurrentRow.Index;
            string dataname = skinDataGridView2.Rows[a].Cells[0].Value.ToString();
            string sql = "select datacode from data where dataname='" + dataname + "'";
            string filecode = DataAccess.Get_Row1_Col1_Value(sql);
            DialogResult dr = MessageBoxEx.Show("确认上传附件吗？", "提示", MessageBoxButtons.OKCancel);
            if (dr == DialogResult.OK)
            {
                string name = appendname.Text;//附件名称(自定义的)
                string type = appendtype.SelectedItem.ToString();//附件类型(自定义的)
                string filetype = Path.GetExtension(this.skinTextBox1.Text).ToLower();//得到该文件的路径的扩展名如.txt

                /*if (appendname1.Text.Trim() == "")
                {
                    MessageBoxEx.Show("附件名称不能为空！", "输入提示");
                    return;
                }*/
                if (this.skinTextBox1.Text == "")
                {
                    MessageBoxEx.Show("请选择文件！", "输入提示");
                    return;
                }
                if (appendtype.SelectedIndex == 0)
                {
                    if (filetype != ".doc" && filetype != ".docx" && filetype != ".pdf")
                    {
                        MessageBoxEx.Show("附件类型不是文档，请修改！", "输入提示");
                        return;
                    }
                }
                if (appendtype.SelectedIndex == 1)
                {
                    if (filetype != ".bmp" && filetype != ".gif" && filetype != ".jpeg" && filetype != ".jpg" && filetype != ".png" && filetype != ".tif" && filetype != ".tiff")
                    {
                        MessageBoxEx.Show("附件类型不是图片，请修改！", "输入提示");
                        return;
                    }
                }
                if (appendtype.SelectedIndex == 2)
                {

                    if (filetype != ".zip")
                    {
                        MessageBoxEx.Show("附件类型不是矢量数据，请修改！", "输入提示");
                        return;
                    }
                }
                if (appendtype.SelectedIndex == 3)
                {

                    if (filetype != ".csv")
                    {
                        MessageBoxEx.Show("附件类型不是三维数据请修改！", "输入提示");
                        return;
                    }
                }
                if (appendtype.SelectedIndex == 4)
                {

                    if (filetype != ".tif")
                    {
                        MessageBoxEx.Show("附件类型不是栅格数据请修改！", "输入提示");
                        return;
                    }
                }
                if (appendtype.SelectedIndex == 5)
                {

                    if (filetype != ".zip")
                    {
                        MessageBoxEx.Show("附件类型不是剖面图数据请修改！", "输入提示");
                        return;
                    }
                }

                sql = "select appendname from append where datacode=" + filecode ;
                MessageBox.Show(filecode);
                string resultquery = DataAccess.Get_Row1_Col1_Value(sql);
                if (resultquery ==appendname.Text)
                {
                    MessageBoxEx.Show("附件名称不能重复，请检查！", "输入提示");
                    return;
                }
                LoadingHelper.ShowLoadingScreen();
                FileStream fs = new FileStream(this.skinTextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(fs);
                Byte[] byData = br.ReadBytes((int)fs.Length);


                //sql = "insert into geodata_file(filecode,prjcode,datacode,appendname,appendtype,appendfile,appendfiletype) values("+filecode+","+ Globale.project_code + "," +subcode + ","+name+","+type + ","+byData+","+filetype + ")";
                /*sql = "insert into data(datatype) values('" + type + "')";
                int i =DataAccess.Run_SQL(sql);
                if (i == 0)
                {
                    MessageBoxEx.Show("类型上传失败", "提示");
                }*/

                //                      子区代码  prjcode  
                sql = "insert into append(datacode,appendname,appendtype,appenddata,append_x,append_y) values(?,?,?,?,?,?)";
                SQLiteParameter[] parameters = {
                    new SQLiteParameter("?datacode",DbType.Int32),
                    //new SQLiteParameter("?prjcode", DbType.Int32),
                    //new SQLiteParameter("?subcode", DbType.String),
                    new SQLiteParameter("?appendname", DbType.String),
                    new SQLiteParameter("?appendtype", DbType.String),
                    new SQLiteParameter("?appenddata", DbType.Binary),
                    new SQLiteParameter("?append_x", DbType.String),
                    new SQLiteParameter("?append_y", DbType.String),
                    /*new SQLiteParameter("?appendfiletype", DbType.String)*/};

                //parameters[0].Value = Globale.project_code;
                //parameters[1].Value = Globale.project_code;
                parameters[0].Value = filecode;
                parameters[1].Value = name;
                parameters[2].Value = type;
                parameters[3].Value = byData;
                parameters[4].Value = appendxBox.Text;
                parameters[5].Value = appendyBox.Text;
                //parameters[6].Value = filetype;

                int result = DataAccess.Run_SQLParameter(sql, parameters);
                LoadingHelper.CloseForm();
                if (result == 1)
                {
                    int index = skinDataGridView1.Rows.Add();
                    skinDataGridView1.Rows[index].Cells[0].Value = name;
                    skinDataGridView1.Rows[index].Cells[1].Value = type;
                    skinDataGridView1.Rows[index].Cells[3].Value = filecode;
                    //skinDataGridView1.Rows[index].Cells[4].Value = filetype;
                    MessageBoxEx.Show("附件上传成功", "提示");
                    this.skinTextBox1.Text = "";
                    
                }
                else
                {
                    MessageBoxEx.Show("附件上传失败", "提示");
                }
            }
        }

        private void SkinDataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void skinComboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string sql = "select subcode from subgeo where subname='"+skinComboBox1.Text+"'";
            int subcode = Convert.ToInt32(DataAccess.Get_Row1_Col1_Value(sql));
            sql = "select dataname,datasource,datadescribe from data where subcode="+subcode;
            DataTable dt = DataAccess.Get_DataTable(sql,"data");
            skinDataGridView2.DataSource = dt;
        }

        private void Form_DataEdit_Load(object sender, EventArgs e)
        {
            string sql = "select subname from subgeo where prjcode=" + Globale.project_code;
            if (DataAccess.Run_SQL(sql) != 0)
            {
                skinComboBox1.Items.Clear();
                DataTable dt = DataAccess.Get_DataTable(sql, "subname");
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        skinComboBox1.Items.Add(dt.Rows[i][0].ToString());//左边下拉框添加项
                    }
                }
            }
        }
    }
}
